package com.csc531;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.csc531.edit.FriendsEdit;
import com.csc531.managers.FriendManager;

public class Friends extends ListActivity
{
	private ListAdapter mAdapter;
	private static final String TAG = "MyActivity";
	private FriendManager friendManager;
	
	//activity results
	private static final int ACTIVITY_CREATE = 0;

	//menu
	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.menu_insert_friend);
		menu.add(0, DELETE_ID, 0,  R.string.menu_delete_friend);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)	{
		switch(item.getItemId()) 
		{
		case INSERT_ID:
			createFriend();
			return true;
		case DELETE_ID:
			deleteFriendFromApplication(getListView().getSelectedItemPosition());
			fillFriendList();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		fillFriendList();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 	{
		super.onCreate(savedInstanceState);

		friendManager = new FriendManager(getContentResolver());
		fillFriendList();	
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) 	{
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(Intent.ACTION_CALL);
		
		String name = (String) mAdapter.getItem(position);
		String phoneId = friendManager.getContactsPhoneNumberByName(name);

		i.setData(Uri.parse("tel:"+phoneId));
		startActivity(i);
	}

	/**
	 * Populate the friend's list
	 */
	private void fillFriendList() 
	{
		ArrayList<String> tempContacts = new ArrayList<String>();

		try {
			tempContacts = friendManager.getFriendsList();			
		}
		catch(Exception ex) {
			Log.v(TAG, "No contacts available...");
		}	

		String [] finalContacts =  new String[tempContacts.size()];
		for(int i=0 ; i < tempContacts.size(); i++) {
			finalContacts[i] = tempContacts.get(i);
		}	
		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, finalContacts);
		setListAdapter(mAdapter);
	}
	
	private void createFriend() {
		Intent i = new Intent(this, FriendsEdit.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	private void deleteFriendFromApplication(int position) {
		try {
			String name = (String) mAdapter.getItem(position);
			long id = friendManager.getContactId(name);

			if(friendManager.deleteContact(id, this)) {
				Toast.makeText(this, R.string.success_delete_friend, Toast.LENGTH_SHORT).show();;
			}else{
				Toast.makeText(this, R.string.failure_delete_friend, Toast.LENGTH_SHORT).show();
			}
		}
		catch(IndexOutOfBoundsException ex) {
			Log.v(TAG,"No contact selected...");
		}
	}
}

